﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace SoundPlugin.Custom_Buttons
{
    /// <summary>
    /// Interaction logic for TriangleButton.xaml
    /// </summary>
    public partial class TriangleButton : UserControl 
    {
        public enum TriangleDirection { Up, Down, Left, Right };
        private static PointCollection[] TrianglePointCollections = 
            {
                new PointCollection(new Point[] {new Point(0, 100) , new Point(50, 0) , new Point(100 , 100)}),
                new PointCollection(new Point[] {new Point(0, 0) , new Point(100, 0) , new Point(50 , 100)}),
                new PointCollection(new Point[] {new Point(0, 50) , new Point(100, 0) , new Point(100 , 100)}),
                new PointCollection(new Point[] {new Point(0, 0) , new Point(0, 100) , new Point(100 , 50)})
            };

        private FrameworkElementFactory triangle;
        private FrameworkElementFactory grid;
        private ControlTemplate controlTemplate;
        private System.Windows.Media.SolidColorBrush borderBrush;
        private System.Windows.Media.SolidColorBrush fillBrush;

        public TriangleButton(TriangleDirection direction , System.Drawing.Color borderColor, System.Drawing.Color fillColor, int borderThickness)
        {
            InitializeComponent();
            this.controlTemplate = new ControlTemplate(typeof(UserControl));

            this.triangle = new FrameworkElementFactory(typeof(Polygon));
            System.Windows.Media.PointCollection points = TrianglePointCollections[(int)direction];
            this.triangle.SetValue(Polygon.PointsProperty, points);
            this.triangle.SetValue(Polygon.MarginProperty, new Thickness(borderThickness));
            this.borderBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(borderColor.A, borderColor.R, borderColor.G, borderColor.B));
            this.fillBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(fillColor.A, fillColor.R, fillColor.G, fillColor.B));
            this.triangle.SetValue(Polygon.StrokeProperty, this.borderBrush);
            this.triangle.SetValue(Polygon.FillProperty, this.fillBrush);
            this.grid = new FrameworkElementFactory(typeof(Grid));
            grid.SetValue(Grid.MarginProperty, new Thickness(5));
            grid.AppendChild(triangle);
            this.controlTemplate.VisualTree = grid;
            this.Template = this.controlTemplate;
            //this.MouseLeftButtonUp += new MouseButtonEventHandler(HandleMouseLeftButtonUp);
        }

        private void HandleMouseLeftButtonUp(object sender, EventArgs e) 
        {
            this.triangle.SetValue(Polygon.StrokeProperty, Brushes.Gold);
            this.triangle.SetValue(Polygon.FillProperty, Brushes.White);
            Thread.Sleep(500);
            this.triangle.SetValue(Polygon.StrokeProperty, this.borderBrush);
            this.triangle.SetValue(Polygon.FillProperty, this.fillBrush);
        }
    }

}
